package com.wander.nio;

import java.io.*;
import java.lang.reflect.Field;

public class FDExample {
    public static void main(String[] args) {
        File file = new File("D:\\code\\architecture_course\\nio\\test.txt");
        try {
            FileInputStream fis = new FileInputStream(file);
            FileDescriptor fd = fis.getFD();
            // 通过反射访问字段（平台相关！）
            Class<?> clazz = fd.getClass();
            Field field;
            try {
                field = clazz.getField("fd");
            } catch (NoSuchFieldException e) {
                // 若失败，尝试Windows系统的"handle"字段
                field = clazz.getDeclaredField("handle");
            }
            field.setAccessible(true);
            long fdValue = field.getLong(fd);
            System.out.println("文件描述符数值：" + fdValue);
        } catch (IOException | NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
